package com.xcl.controller;

import com.xcl.pojo.Article;
import com.xcl.pojo.Category;
import com.xcl.pojo.Result;
import com.xcl.service.impl.ArticleServiceImpl;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cate")//避免请求同名冲突
public class ArticeController {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ArticeController.class);
    @Autowired
    ArticleServiceImpl articleService;

    //查询所有
    @RequestMapping("/findArticles")
    public Result findArticle(@RequestParam(required = false) Integer categoryId,@RequestParam(required = false) String state){
        log.info("进入:{}", "findArticles");
        List<Article> lst = articleService.findArticle(categoryId, state);
        return Result.success(lst);
    }

    //删除数据
    @RequestMapping("/deleteById")
    public Result deleteId(Integer id){
        return articleService.deleteId(id);
    }

    //添加数据
    @RequestMapping("/add")
    public Result addArticle(@RequestBody Article article){
        Integer num = articleService.addArticle(article);
        if(num > 0){
            return Result.success();
        }
        return Result.error("新增失败");
    }

    //修改文章
    @RequestMapping("/update")
    public Result updateArticle(@Valid @RequestBody Article article){
        Integer num = articleService.updateArticle(article);
        if(num > 0){
            return Result.success();
        }
        return Result.error("修改失败");
    }
}
